<?php

class AhpRoleResource extends Eloquent
{

    protected $table = "ahp_role_resource";

    
    /**
     * @Author:      dx
     * @DateTime:    2016-07-14 15:16:42
     * @Description: 获取该角色拥有的额 状态正常的资源 不进行角色状态判断 角色状态 在角色维护时候对拥有该角色的用户进行判断
     */
    public static function getRoleResource($role_id) {
    	$result = DB::table('ahp_role_resource')
    	->join('ahp_resource', 'ahp_role_resource.resource_id', '=', 'ahp_resource.id')
    	->where('ahp_role_resource.role_id', $role_id)
        ->where('ahp_resource.status', 1)->where('ahp_resource.pid','!=', 0);
            
        $result = $result->select('ahp_role_resource.id','ahp_role_resource.resource_id', 'ahp_role_resource.role_id',
            'ahp_resource.url','ahp_resource.name as resname',
            'ahp_resource.status', 'ahp_resource.id as resid', 'ahp_resource.pid as rpid')
        ->orderBy("ahp_resource.sort","asc")->get();
        $arr=array();
        foreach ($result as $r) {
        	$arr[]=$r->url;
        }
        return $arr;
    }

	/**
	 * @Author:      dx
	 * @DateTime:    2016-07-14 15:20:59
	 * @Description: 获取指定 角色拥有的 菜单  角色状态进行判断
	 */
	public static function getSystemMenu($role_id) {
		$result = DB::table('ahp_role_resource')
    	->join('ahp_resource', 'ahp_role_resource.resource_id', '=', 'ahp_resource.id')
    	->where('ahp_role_resource.role_id', $role_id)
        ->where('ahp_resource.status', 1)->where('ahp_resource.pid',0);
        $result = $result->select('ahp_resource.*')->orderBy("ahp_resource.sort","asc")->get();//获得有权限的目录
        $menu=array();
        foreach ($result as  $r){
        	$_r =DB::table('ahp_role_resource')
	    	->join('ahp_resource', 'ahp_role_resource.resource_id', '=', 'ahp_resource.id')->where('ahp_role_resource.role_id', $role_id)
	        ->where('ahp_resource.status', 1)->where('ahp_resource.pid',$r->id)->where("ahp_resource.type",2);
	        $_r = $_r->select('ahp_resource.*')->orderBy("ahp_resource.sort","asc")->get();//获得有权限的目录
        	$r->son=$_r;
        	$menu[]=$r;
        }
		return $menu;
	}
	/**
	 * @Author:      kc
	 * @DateTime:    2016-08-02 16:02:59
	 * @Description: 获取经销商 角色拥有的 菜单  角色状态进行判断
	 */
	public static function getArcSystemMenu($role_id,$src = 2) {
		$result = DB::table('ahp_role_resource')
				->join('ahp_resource', 'ahp_role_resource.resource_id', '=', 'ahp_resource.id')
				->where('ahp_role_resource.role_id', $role_id)
				->where('ahp_resource.status', 1)->where('ahp_resource.pid',0)->where('ahp_resource.source',$src);
		$result = $result->select('ahp_resource.*')->orderBy("ahp_resource.sort","asc")->get();//获得有权限的目录
		$menu=array();
		foreach ($result as  $r){
			$_r =DB::table('ahp_role_resource')
					->join('ahp_resource', 'ahp_role_resource.resource_id', '=', 'ahp_resource.id')->where('ssp_role_resource.role_id', $role_id)
					->where('ahp_resource.status', 1)->where('ahp_resource.pid',$r->id)->where('ahp_resource.source',$src);
			$_r = $_r->select('ahp_resource.*')->orderBy("ahp_resource.sort","asc")->get();//获得有权限的目录
			$r->son=$_r;
			$menu[]=$r;
		}
		return $menu;
	}
	/**
	 * 判断页面按钮是否有权限
	 * @param $butaction 按钮的action        	
	 * @return true 有权限 false 没有权限
	 */
	public static function isButtShow($butaction, $city = 0) {
		$key = self::getCacheKey()['res_key'];
		try {
			$resource_arr = Cache::get ( $key );

			if (in_array ( $butaction, $resource_arr )) { // 如果有权限 继续下去
				return true;
			} else {
				// 没有权限
				return false;
			}
		 } catch ( Exception $e ) {
			return false;
		 }
	}
	
	/**
	 * 返回url，去除参数
	 * @return url
	 */
	public static function getUrl() {
		$route = Route::getCurrentRoute();
		$path = $route->getPath();		// chesudi/home/index/{id}
		$prefix = $route->getPrefix();	// chesudi
		$url = empty($prefix) ? $path : str_replace($prefix.'/', '', $path);
        $url = preg_replace('/(.+)\/(\{.+\})/i', '$1', $url);
        return $url;
    }


}